package com.game.framework.orm.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;

import org.apache.log4j.Logger;

import com.game.utils.log.Log;


/**
 * ConnectionProvider
 * @author Liyuan
 */

public class ConnectionProvider {
	/** logger */
	private static Logger logger 		= Logger.getLogger(ConnectionProvider.class);
	/** url */
	private String url 					= null;
	/** loginTimeout */
	private Integer loginTimeout 		= 10;
	/** driver */
	private static final String driver 	= "com.mysql.jdbc.Driver";
	
	/** Constructor */
	public ConnectionProvider(String url) {
		this.url = url;
	}
	
	/** getConnection */
	public Connection getConnection(){
		try {
			if(url == null || "".equals(url)) return null;
			Class.forName(driver);
			Connection conn = DriverManager.getConnection(url);
			conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); //事务隔离级别
			DriverManager.setLoginTimeout(loginTimeout); //超时
			//DriverManager.setLogWriter(new PrintWriter(System.out, true)); //日志
			logger.debug("Create Connection Success.");
			return conn;
		} catch (Exception e) {
			logger.error("Create Connection Failed.");
			Log.printStackTrace(e);
		}
		return null;
	}
	
}
